import Vue from 'vue'
import Vuex from 'vuex'

import router from './router'

Vue.use(Vuex)

const isLogin = Boolean(sessionStorage.getItem('isLogin'))
const userInfo = JSON.parse(sessionStorage.getItem('userInfo')) || {}

export default new Vuex.Store({
	state: {
		// 用户信息
		userInfo,

		token: '',

		isLogin
	},

	actions: {
		async setUserInfo({commit}) {
			const {success, data, status} = await axios.get('/api/auth/status', {
				baseURL: ''
			})

			if (! success) {
				return
			}

			commit('setUserInfo', data)
		}
	},

	mutations: {
		setUserInfo(state, value) {
			state.userInfo = value
		},
		setToken(state, value) {
			state.token = value
		},
		setIsLogin(state, value) {
			state.isLogin = value
		}
	}
})
